export function timeAgoConvert(dateTimeStamp: any) {

    try {
        let result = "";
        let minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
        let hour = minute * 60;
        let day = hour * 24;
        let week = day * 7;
        let month = day * 30;
        let year = day * 365;
        let now = new Date().getTime();   //获取当前时间毫秒
        dateTimeStamp = dateTimeStamp.substring(0, 18);
        //必须把日期'-'转为'/'
        dateTimeStamp = dateTimeStamp.replace(/-/g, '/');

        let timestamp = new Date(dateTimeStamp).getTime();

        let diffValue = now - timestamp;//时间差

        if (diffValue < 0) {
            return result;
        }
        let minC = diffValue / minute;  //计算时间差的分，时，天，周，月
        let hourC = diffValue / hour;
        let dayC = diffValue / day;
        let weekC = diffValue / week;
        let monthC = diffValue / month;
        let yearC = diffValue / year;

        minC = parseInt(String(minC))
        hourC = parseInt(String(hourC))
        dayC = parseInt(String(dayC))
        weekC = parseInt(String(weekC))
        monthC = parseInt(String(monthC))
        yearC = parseInt(String(yearC))

        if (monthC > 12) {
            result = " " + yearC + "年前"
        } else if (monthC >= 1 && monthC < 12) {
            result = " " + monthC + "月前"
        } else if (weekC >= 1 && weekC <= 4) {
            result = " " + weekC + "周前"
        } else if (dayC >= 1 && dayC <= 6) {
            result = " " + dayC + "天前"
        } else if (hourC >= 1 && hourC <= 23) {
            result = " " + hourC + "小时前"
        } else if (minC >= 1 && minC <= 59) {
            result = " " + minC + "分钟前"
        } else if (diffValue >= 0 && diffValue <= minute) {
            result = "刚刚"
        }
        return result;
    } catch (e) {
        return "刚刚"
    }
}

export function monthDay(y: any, m: any, weekStartProps: number) {
    let firstDayOfMonth = new Date(y, m - 1, 1).getDay(); // 当月第一天星期几
    let lastDateOfMonth = new Date(y, m, 0).getDate(); // 当月最后一天
    let lastDayOfLastMonth = new Date(y, m, 0).getDate(); // 上一月的最后一天
    let dates = []; // 所有渲染日历
    let weekStart = weekStartProps == 7 ? 0 : weekStartProps; // 方便进行日期计算，默认星期从0开始
    let startDay = (() => {
        // 周初有几天是上个月的
        if (firstDayOfMonth == weekStart) {
            return 0;
        } else if (firstDayOfMonth > weekStart) {
            return firstDayOfMonth - weekStart;
        } else {
            return 7 - weekStart + firstDayOfMonth;
        }
    })();
    let endDay = 7 - ((startDay + lastDateOfMonth) % 7); // 结束还有几天是下个月的
    for (let i = 1; i <= startDay; i++) {
        dates.push({
            date: lastDayOfLastMonth - startDay + i,
            day: weekStart + i - 1 || 7,
            month: m - 1 >= 0 ? m - 1 : 12,
            year: m - 1 >= 0 ? y : y - 1
        });
    }
    for (let j = 1; j <= lastDateOfMonth; j++) {
        dates.push({
            date: j,
            day: (j % 7) + firstDayOfMonth - 1 || 7,
            month: m,
            year: y,
            lm: true
        });
    }
    if (endDay < 7){
        for (let k = 1; k <= endDay; k++) {
            dates.push({
                date: k,
                day: (lastDateOfMonth + startDay + weekStart + k - 1) % 7 || 7,
                month: m + 1 <= 11 ? m + 1 : 0,
                year: m + 1 <= 11 ? y : y + 1
            });
        }
    }
    return dates;
}

export function dateFormat(d: any, fmt: any) {
    let D = new Date(d);
    let o: any = {
        "M+": D.getMonth() + 1, //月份
        "d+": D.getDate(), //日
        "h+": D.getHours(), //小时
        "m+": D.getMinutes(), //分
        "s+": D.getSeconds(), //秒
        "q+": Math.floor((D.getMonth() + 3) / 3), //季度
        S: D.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (D.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1
                    ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length)
            );
    return fmt;
}
